import math
import numpy as np
import matplotlib.pyplot as plt
from itertools import permutations, combinations
from collections import Counter
import scipy.stats as stats

def set_chinese_font():
    """设置中文字体"""
    plt.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei', 'DejaVu Sans']
    plt.rcParams['axes.unicode_minus'] = False  # 解决负号显示问题

set_chinese_font()

def trigonometry():
    print("=== 三角函数 ===")
    
    # 基本三角函数
    x = np.linspace(-2*np.pi, 2*np.pi, 400)
    
    y_sin = np.sin(x)
    y_cos = np.cos(x)
    y_tan = np.tan(x)
    
    # 三角恒等式验证
    angle = np.pi/4  # 45度
    identity1 = np.sin(angle)**2 + np.cos(angle)**2
    identity2 = np.sin(2*angle) - 2*np.sin(angle)*np.cos(angle)
    
    print(f"恒等式验证:")
    print(f"  sin²({angle:.2f}) + cos²({angle:.2f}) = {identity1:.6f} (应为1)")
    print(f"  sin(2×{angle:.2f}) - 2sin({angle:.2f})cos({angle:.2f}) = {identity2:.6f} (应为0)")
    
    # 可视化
    fig, axes = plt.subplots(2, 2, figsize=(12, 10))
    
    # 基本三角函数
    axes[0, 0].plot(x, y_sin, 'r-', label='y = sin(x)')
    axes[0, 0].set_title('正弦函数')
    axes[0, 0].grid(True)
    axes[0, 0].legend()
    
    axes[0, 1].plot(x, y_cos, 'b-', label='y = cos(x)')
    axes[0, 1].set_title('余弦函数')
    axes[0, 1].grid(True)
    axes[0, 1].legend()
    
    # 正切函数（排除奇点）
    x_tan = x[np.abs(np.cos(x)) > 0.01]  # 排除cos(x)接近0的点
    y_tan_filtered = np.tan(x_tan)
    axes[1, 0].plot(x_tan, y_tan_filtered, 'g-', label='y = tan(x)')
    axes[1, 0].set_ylim(-5, 5)
    axes[1, 0].set_title('正切函数')
    axes[1, 0].grid(True)
    axes[1, 0].legend()
    
    # 单位圆
    theta = np.linspace(0, 2*np.pi, 100)
    x_circle = np.cos(theta)
    y_circle = np.sin(theta)
    
    angle_show = np.pi/3  # 60度
    x_point = np.cos(angle_show)
    y_point = np.sin(angle_show)
    
    axes[1, 1].plot(x_circle, y_circle, 'k-', linewidth=2)
    axes[1, 1].plot([0, x_point], [0, y_point], 'r-', linewidth=2)
    axes[1, 1].plot(x_point, y_point, 'ro', markersize=8)
    axes[1, 1].text(x_point+0.1, y_point+0.1, f'({x_point:.2f}, {y_point:.2f})')
    axes[1, 1].axhline(0, color='black', linewidth=0.5)
    axes[1, 1].axvline(0, color='black', linewidth=0.5)
    axes[1, 1].set_xlim(-1.5, 1.5)
    axes[1, 1].set_ylim(-1.5, 1.5)
    axes[1, 1].set_aspect('equal')
    axes[1, 1].set_title('单位圆')
    axes[1, 1].grid(True)
    
    plt.tight_layout()
    plt.show()

trigonometry()